package com.cn.controller;

import com.cn.configuration.SpringUtils;
import com.cn.entity.Log;
import com.cn.service.DataSourceService;
import io.krakens.grok.api.Grok;
import io.krakens.grok.api.GrokCompiler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.boot.jackson.JsonObjectDeserializer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author chenming
 * Date  2022/9/26 10:42
 * @version 1.0
 * description
 */
@Controller
@Api(tags = "the toWord API")
public class TestController {

//    @GetMapping("test")
//    public void test() {
//       DataSourceService dataSourceService = SpringUtils.getBean("AttackServiceImpl");
//       dataSourceService.execute();
//    }

    @GetMapping("test2")
    public void getGrok(@RequestBody Log log) {
        GrokCompiler grokCompiler = GrokCompiler.newInstance();
        try {
            Grok grok = grokCompiler.compile("%{GREEDYDATA}%{IP:deviceAddress}\\s+%{GREEDYDATA:deviceName}:触发时间：%{GREEDYDATA:occurTimeStr}\\s+链路名称：%{GREEDYDATA} 警报名称：%{GREEDYDATA:eventName} 警报等级：%{GREEDYDATA:originLevel} 警报种类：%{GREEDYDATA:originType} 触发条件：%{GREEDYDATA:eventDiest} 警报类型：%{GREEDYDATA:sourceName} 源IP地址：%{IP:sourceAddress} 源端口：%{INT:sourcePort} 目标IP地址：%{IP:destinationAddress} 目标端口：%{INT:destinationPort} 累计触发次数：%{INT:mergeCount} extendString9：%{GREEDYDATA:extendString9} extendString10：%{GREEDYDATA:extendString10} extendString8：%{GREEDYDATA:extendString8} operation：%{GREEDYDATA:operation}");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 将 swagger 文档一键下载为 doc 文档
     *
     * @param model
     * @param url      需要转换成 word 文档的资源地址
     * @param response
     */
    @RequestMapping(value = "/downloadWord", method = {RequestMethod.GET})
    public void word(Model model, @RequestParam(required = false) String url, HttpServletResponse response) {
        url = "http://"+url+"/v2/api-docs";
//        generateModelData(model, url, 0);
//        writeContentToResponse(model, response);
    }

    public static void main(String[] args) {
        String a = "123|";
        System.out.println(a.substring(0,a.indexOf("|")));
    }

}
